package org.zjt.hdfs.demo;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.SequenceFile.Reader;
import org.apache.hadoop.io.Text;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;

public class HDFSTest {

    public Configuration config = new Configuration();
    public FileSystem fs;

    /**
     * master 	为namenode节点
     * root	  	为root权限
     *
     * @throws Exception
     */
    @Before
    public void setup() throws Exception {
        fs = FileSystem.get(new URI("hdfs://master:9000/"), config, "root");
    }

    @After
    public void end() throws Exception {
        if (fs != null) {
            fs.close();
        }
    }

    @Test
    public void mkdir() throws Exception {
        Path path = new Path("/test/zs");
        fs.mkdirs(path);
    }

    @Test
    public void upload() throws Exception {
        Path file = new Path("/usr/input/wc.txt");
        FSDataOutputStream out = fs.create(file);
//		IOUtils.copy(input, output)
        org.apache.hadoop.io.IOUtils.copyBytes(new FileInputStream("/Users/zhangjuntao/IdeaProjects/myproject/mapreducer/data/wc.txt"), out, config);

    }

    @Test
    public void list() throws Exception {
        Path path = new Path("/usr/input/");
        FileStatus[] fss = fs.listStatus(path);
        for (FileStatus file : fss) {
            System.out.println(file.isDirectory());
            System.out.println(file.getPath().getName());
        }
    }

    @Test
    public void smallfile() throws Exception {
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, config, new Path("/test/zs/test.seq"), Text.class, Text.class, CompressionType.NONE);

        File dir = new File("F:\\data");
        for (File file : dir.listFiles()) {
            writer.append(new Text(file.getName()), new Text(FileUtils.readFileToString(file)));
        }
        writer.close();
    }

    @Test
    public void readsmallfile() throws Exception {
        SequenceFile.Reader read = new Reader(fs, new Path("/test/zs/test.seq"), config);
        Text value = new Text();
        while (read.next(new Text("NOTICE.txt"), value)) {
            System.out.println(value.toString());
        }
    }
}
